package cisdi.mes.sm.rm.daoImpl;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cisdi.mes.sm.rm.dao.MeasureDocLogDao;
import cisdi.mes.sm.rm.domain.SmRmMeasureDoc;
import cisdi.mes.sm.rm.domain.SmRmMeasureDocLog;
import cisdi.mes.sm.rm.domain.SmRmStorageContent;
import cisdi.mes.sm.util.base.SysConst;

@Repository
public class MeasureDocLogDaoImpl extends HibernateDaoSupport implements
		MeasureDocLogDao {

	@Autowired
	public void setMySessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	@Override
	public void saveLog(SmRmMeasureDocLog measureDocLog) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(measureDocLog);
	}

	@Override
	public void logMatRcv(SmRmMeasureDoc measureDoc,HttpServletRequest request) {
			SmRmMeasureDocLog log = new SmRmMeasureDocLog();
			log.setIdAddr(request.getRemoteAddr());
			log.setMeasureId(measureDoc.getMeasureId());
			log.setMaterialId(measureDoc.getMaterialId());
			log.setEvent("RECEIVE");
			log.setWeight(measureDoc.getNetWeight());
			this.getHibernateTemplate().save(log);
		
	}

	@Override
	public void logMatRtn(SmRmMeasureDoc measureDoc,HttpServletRequest request) {
			SmRmMeasureDocLog log = new SmRmMeasureDocLog();
			log.setMeasureId(measureDoc.getMeasureId());
			log.setIdAddr(request.getRemoteAddr());
			log.setMaterialId(measureDoc.getMaterialId());
			log.setEvent("RETURN");
			log.setWeight(measureDoc.getNetWeight());
			this.getHibernateTemplate().save(log);
		
	}

	@Override
	public void logMatUnload(SmRmMeasureDoc md, String storageId, Double wgt,HttpServletRequest request) {
		SmRmMeasureDocLog log = new SmRmMeasureDocLog();
		log.setMeasureId(md.getMeasureId());
		log.setMaterialId(md.getMaterialId());
		log.setIdAddr(request.getRemoteAddr());
		log.setWeight(wgt);
		log.setEvent("STOCK");
		log.setFromStorageId(SysConst.WAREHOUSE_MAT);
		log.setToStorageId(storageId);
		this.getHibernateTemplate().save(log);
	}


	@Override
	public void logMatLoad(String to, SmRmStorageContent from, Double wgt,HttpServletRequest request) {
		SmRmMeasureDocLog log = new SmRmMeasureDocLog();
		log.setMeasureId(from.getMeasureId());
		log.setMaterialId(from.getMaterialId());
		log.setIdAddr(request.getRemoteAddr());
		log.setWeight(wgt);
		log.setEvent("LOAD");
		log.setFromStorageId(from.getStorageId());
		log.setToStorageId(to);
		this.getHibernateTemplate().save(log);
		
	}

	@Override
	public void logMatLoad(String to, SmRmStorageContent from,HttpServletRequest request) {
		SmRmMeasureDocLog log = new SmRmMeasureDocLog();
		log.setMeasureId(from.getMeasureId());
		log.setMaterialId(from.getMaterialId());
		log.setIdAddr(request.getRemoteAddr());
		log.setWeight(from.getWeight());
		log.setEvent("LOAD");
		log.setFromStorageId(from.getStorageId());
		log.setToStorageId(to);
		this.getHibernateTemplate().save(log);		
	}

	@Override
	public void logUndoLoadBin(SmRmStorageContent bContent,HttpServletRequest request) {
		SmRmMeasureDocLog log = new SmRmMeasureDocLog();
		log.setMeasureId(bContent.getMeasureId());
		log.setMaterialId(bContent.getMaterialId());
		log.setWeight(bContent.getWeight());
		log.setIdAddr(request.getRemoteAddr());
		log.setEvent("UNDO-LOAD");
		log.setFromStorageId(bContent.getStorageId());
		log.setToStorageId(bContent.getFromStorageId());
		this.getHibernateTemplate().save(log);	
		
	}

}
